package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmNewsMapper wmNewsMapper;
    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 分页模糊查询频道
     * @return
     */
    public ResponseResult listAll(ChannelDto dto){
        dto.checkParam();

        IPage<WmChannel> page=new Page<WmChannel>(dto.getPage(), dto.getSize());
        QueryWrapper<WmChannel> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like("name",dto.getName());
        }

        page=page(page,wrapper);
        if (dto.getPage()>page.getPages()){
            IPage<WmChannel> page2=new Page<>(page.getPages(),dto.getSize());
            page = page(page2, wrapper);
        }

        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }

    /**
     * 新增频道
     * @return
     */
    public ResponseResult saveChannel(WmChannel wmChannel){
        if (StringUtils.isBlank(wmChannel.getName())){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(false);

        boolean save = save(wmChannel);
        if (!save){
            throw new CustomException(AppHttpCodeEnum.FAIL_TO_SAVE_CHANNEL);
        }
        return new ResponseResult();
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    public ResponseResult deleteChannel(Integer id){
        if(id==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        QueryWrapper<WmChannel> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        WmChannel channel = getOne(wrapper);
        if (channel==null){
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }

        //若频道下有文章，则删除失败
        QueryWrapper<WmNews> wrapper1=new QueryWrapper<WmNews>();
        wrapper1.eq("channel_id",id);
        List<WmNews> wmNewsList = wmNewsMapper.selectList(wrapper1);
        if (wmNewsList!=null&&wmNewsList.size()!=0){
            throw new CustomException(AppHttpCodeEnum.CHANNEL_HAS_BEEN_USE);
        }

        boolean b = removeById(id);
        if (!b){
            throw new CustomException(AppHttpCodeEnum.FAIL_TO_DELETE_CHANNEL);
        }

        return new ResponseResult<>();
    }

    /**
     * 编辑频道
     * @return
     */
    public ResponseResult updateChannel(WmChannel wmChannel){
        if (wmChannel.getId()==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (wmChannel.getName()==""){
            throw new CustomException(AppHttpCodeEnum.CHANNEL_NAME_NULL);
        }

        boolean b = updateById(wmChannel);
        if (!b){
            throw new CustomException(AppHttpCodeEnum.FAIL_TO_UPDATE_CHANNEL);
        }
        return new ResponseResult<>();
    }
}